//
//  TitleBlockView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/6.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit
protocol reportHandle {
    func handleClick(clicked : Int) -> Void
}
class TitleBlockView: BaseView {
    var param : Dictionary<String,Any>?
    var delegate : reportHandle?
    var clicked : Bool = false
    var multiChose : Bool = false
    var extr : Bool = false //是否需要额外检测屏幕按钮
    init(height : CGFloat,title : String,titleArr : Array<String>,key : String,multiChoose : Bool,extra : Bool  = false) {
        super.init(frame: CGRect.init(origin: CGPoint.init(x: 0, y: height), size: CGSize.init(width: YXDeviceWidth - 20 * YXWidthScale, height: 0)))
        extr = extra
        let lbl = UILabel.init()
        lbl.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        lbl.textColor = UIColor.colorWithHexString(hex: "323232")
        lbl.text = title
        let size = title.StringSize(font: lbl.font)
        lbl.frame = CGRect.init(origin: CGPoint.init(x: 40 * YXWidthScale, y: 40 * YXHeightScale), size: size)
        self.addSubview(lbl)
        let titleLbl = UILabel.init()
        titleLbl.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        titleLbl.textColor = UIColor.colorWithHexString(hex: "323232")
        titleLbl.frame = CGRect.zero
        titleLbl.text = ""
        self.addSubview(titleLbl)
        var idxs = 0
        var bottom : CGFloat = 0.0
        multiChose = multiChoose
        if multiChoose == true {
            param = [key : []]
        } else {
            param = [key : ""]
        }
        for element in titleArr {
            let sure = UIButton.init(type: .system)
            sure.layer.borderColor = UIColor.colorWithHexString(hex: "cccccc").cgColor
            sure.backgroundColor = UIColor.white
            sure.layer.borderWidth = YXWidthScale
            sure.layer.cornerRadius = 4 * YXWidthScale
            sure.setTitle(element, for: .normal)
            sure.setTitleColor(UIColor.colorWithHexString(hex: "909090"), for: .normal)
            sure.titleLabel!.font = UIFont.systemFont(ofSize: 30 * YXWidthScale)
            let originx = (idxs % 2 == 0) ? lbl.left() : self.bounds.size.width - 340 * YXWidthScale
            let originy = lbl.bottom() + 50 * YXHeightScale + CGFloat(100 * (idxs / 2)) * YXHeightScale
            //将一个值设置好
            let index = titleArr.index(of: element)
            sure.tag = 1000 + index!
            sure.setTitleColor(UIColor.white, for: .selected)
            sure.frame = CGRect.init(x: originx, y: originy, width: 300 * YXWidthScale, height: btnHeight)
            self.addSubview(sure)
            if multiChoose == true {
                sure.addTarget(self, action: #selector(multiChoose(btn:)), for: .touchUpInside)
            } else {
                sure.addTarget(self, action: #selector(hehe(btn:)), for: .touchUpInside)
            }
            bottom = sure.bottom()
            idxs = idxs + 1
            //一般情况下是0，1
        }
        let line = UIView.init(frame: CGRect.init(x: 40.0 * YXWidthScale, y: bottom + 20 * YXHeightScale, width: self.bounds.size.width - 40.0 * YXWidthScale, height: YXHeightScale))
        line.backgroundColor = UIColor.colorWithHexString(hex: "ececec")
        self.addSubview(line)
        self.frame = CGRect.init(x: 0, y: self.frame.origin.y, width: self.bounds.size.width, height: line.bottom())
        
    }
    
    init(idx : Int,title : String,titleArr : Array<String>,multiChoose : Bool,key : String,extra : Bool = false){
        super.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: YXDeviceWidth - 20 * YXWidthScale, height: 0)))
        extr = extra
        let lbl = UILabel.init()
        lbl.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        lbl.textColor = UIColor.colorWithHexString(hex: "323232")
        lbl.text = title
        let size = title.StringSize(font: lbl.font)
        lbl.frame = CGRect.init(origin: CGPoint.init(x: 40 * YXWidthScale, y: 40 * YXHeightScale), size: size)
        self.addSubview(lbl)
        let titleLbl = UILabel.init()
        titleLbl.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        titleLbl.textColor = UIColor.colorWithHexString(hex: "323232")
        titleLbl.frame = CGRect.zero
        titleLbl.text = ""
        self.addSubview(titleLbl)
        var idxs = 0
        var bottom : CGFloat = 0.0
        if multiChoose == true {
            param = [key:[]]
        } else {
            param = [key : ""]
        }
        for element in titleArr {
            let sure = UIButton.init(type: .system)
            sure.layer.borderColor = UIColor.colorWithHexString(hex: "cccccc").cgColor
            sure.backgroundColor = UIColor.white
            sure.layer.borderWidth = YXWidthScale
            sure.layer.cornerRadius = 4 * YXWidthScale
            sure.setTitle(element, for: .normal)
            sure.setTitleColor(UIColor.colorWithHexString(hex: "909090"), for: .normal)
            sure.titleLabel!.font = UIFont.systemFont(ofSize: 24 * YXWidthScale)
            let index = titleArr.index(of: element)
            sure.tag = 1000 + index!
            let originx = (idxs % 2 == 0) ? lbl.left() : self.bounds.size.width - 340 * YXWidthScale
            let originy = lbl.bottom() + 50 * YXHeightScale + CGFloat(100 * (idxs / 2)) * YXHeightScale
            sure.frame = CGRect.init(x: originx, y: originy, width: 300 * YXWidthScale, height: btnHeight)
            self.addSubview(sure)
            if multiChoose {
                sure.addTarget(self, action: #selector(multiChoose(btn:)), for: .touchUpInside)
            } else {
                sure.addTarget(self, action: #selector(hehe(btn:)), for: .touchUpInside)
            }
            bottom = sure.bottom()
            idxs = idxs + 1
        }
        let line = UIView.init(frame: CGRect.init(x: 40.0 * YXWidthScale, y: bottom + 20 * YXHeightScale, width: self.bounds.size.width - 40.0 * YXWidthScale, height: YXHeightScale))
        line.backgroundColor = UIColor.colorWithHexString(hex: "ececec")
        self.addSubview(line)
        let botom = line.bottom() * CGFloat(idx - 1)
        self.frame = CGRect.init(x: 0, y: 88 * YXHeightScale + botom, width: self.bounds.size.width, height: line.bottom())
    }
    
    //用于检测屏幕
    func extra(idx : Int) -> Void {
        //0or其它
        //多选情况下会有
        let key = param!.keys.first!
        if idx == 0 {
            if let btn = self.viewWithTag(1000 + idx){
                param![key] = ["0"]
                for item in self.subviews {
                    if item is UIButton {
                        if item != btn {
                            let itm = item as! UIButton
                            itm.backgroundColor = UIColor.white
                            itm.setTitleColor(UIColor.colorWithHexString(hex: "909090"), for: .normal)
                        }
                    }
                }
            }
        }else{
            if let btn = self.viewWithTag(1000) as? UIButton {
                btn.backgroundColor = UIColor.white
                btn.setTitleColor(UIColor.colorWithHexString(hex: "909090"), for: .normal)
            }
            if var array = param![key] as? Array<String> {
                if let id = array.index(of: "0") {
                    array.remove(at: id)
                    param![key] = array
                }
            }
        }
    }
    
    //多选情况，任务完成
    @objc func multiChoose(btn : UIButton) -> Void {
        let tag = btn.tag - 1000
        let value = String(tag)
        let key = param!.keys.first!
        if btn.backgroundColor == UIColor.black {
            btn.backgroundColor = UIColor.white
            btn.setTitleColor(UIColor.colorWithHexString(hex: "909090"), for: .normal)
            var val = param![key] as! Array<String>
            for item in val {
                if item == value {
                    if let idx = val.index(of: item) {
                        val.remove(at: idx)
                        param![key] = val
                    }
                }
            }//第一，将颜色变为黑色标题变为白色,第二，将该纪录从param中删除
        }else {
            //设置颜色 添加纪录
            btn.backgroundColor = UIColor.black
            btn.setTitleColor(UIColor.white, for: .normal)
            var val = param![key] as! Array<String>
            val.append(value)
            param![key] = val
        }
        //如果点击屏幕有缺陷则将屏幕完好按钮设置成未选中状态
        if extr == true {
            extra(idx: tag)
        }
    }
    
    func lightAButton(idx : Int) -> Void {
        let tag = 1000 + idx
        if let btn = self.viewWithTag(tag) as? UIButton {
            light(btn: btn)
        }
    }
    
    func light(btn : UIButton) -> Void {
        clicked = true
        let tag = btn.tag - 1000
        let value = String(tag)
        let key = param!.keys.first!
        
        if multiChose == true {
            if var array = param![key] as? Array<String> {
                array.append(String(tag))
                param![key] = array
            }
        } else {
            param![key] = value
        }
        btn.backgroundColor = UIColor.black
        btn.setTitleColor(UIColor.white, for: .normal)
        //每次点击后其父视图都去判断有多少个按钮被点击了
        delegate?.handleClick(clicked: 0)
    }
    
    //单选的事件
    @objc func hehe(btn: UIButton) -> Void {
        clicked = true
        let tag = btn.tag - 1000
        let value = String(tag)
        let key = param!.keys.first!
        
        if multiChose == true {
            if var array = param![key] as? Array<String> {
                array.append(String(tag))
                param![key] = array
            }
        } else {
            param![key] = value
        }
        
        btn.backgroundColor = UIColor.black
        btn.setTitleColor(UIColor.white, for: .normal)
        for item in self.subviews {
            if item is UIButton {
                if item != btn {
                    let itm = item as! UIButton
                    itm.backgroundColor = UIColor.white
                    itm.setTitleColor(UIColor.colorWithHexString(hex: "909090"), for: .normal)
                }
            }
        }
        //每次点击后其父视图都去判断有多少个按钮被点击了
        delegate?.handleClick(clicked: 0)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
